import {
  queryEnergyHistoryList, queryControlRecordList, queryBurglarproofList,
  actionEnergyHistoryExport, actionControlRecordExport,actionBurglarproofExport,
} from '../services/energy';


export default {
  namespace: 'energy',

  state: {
    resdata: {
      list: [],
      pagination: {},
    },
    loading: {
      reslist: true,
      model_submit:false,
      detailinfo:true,
    },
  },

  effects: {
    // 历史能耗查询
    *fetch_energy_history_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { reslist: true },
      });
      const response = yield call(queryEnergyHistoryList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { reslist: false },
      });
    },

    // 控制记录查询
    *fetch_control_record_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { reslist: true },
      });
      const response = yield call(queryControlRecordList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { reslist: false },
      });
    },

    // 防盗信息查询
    *fetch_burglarproof_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { reslist: true },
      });
      const response = yield call(queryBurglarproofList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { reslist: false },
      });
    },



    // 历史能耗导出
    *action_energy_history_export({ payload }, { call, put }) {
      const response = yield call(actionEnergyHistoryExport, payload);
    },

    // 控制记录导出
    *action_control_record_export({ payload }, { call, put }) {
      const response = yield call(actionControlRecordExport, payload);
    },

    // 防盗信息导出
    *action_burglarproof_export({ payload }, { call, put }) {
      const response = yield call(actionBurglarproofExport, payload);
    },


  },

  reducers: {
    appendData(state, action) {
      return {
        ...state,
        resdata: action.payload,
      };
    },
    appendDependentHistoryWarn(state, action) {
      return {
        ...state,
        dependentHistoryWarn:action.payload,
      };
    },


    changeLoading(state, action) {
      return {
        ...state,
        loading: {
          ...state.loading,
          ...action.payload,
        },
      };
    },
    appendDataDetail(state, action) {
      return {
        ...state,
        detaildata: action.payload,
      };
    },

    appendClearState(state, action) {
      return {
        ...state,
        resdata:{},
        detaildata:{},
      };
    },

  },
};
